package com.tech.connect.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.model.City;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.utils.ParamsUtil;
import com.ksy.oss.OssClient;
import com.ksy.oss.OssResult;
import com.tech.connect.R;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.CurrentInfo;
import com.tech.connect.api.ImageApi;
import com.tech.connect.api.LvXinHttp;
import com.tech.connect.model.ItemCatSub;
import com.tech.connect.util.CatUtils;
import com.tech.connect.view.ImageAdder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PublishLvYouPeiActivity extends BaseActivity {
    private ImageAdder imageAdder;
    private RecyclerView recycler3;
    private List<ItemCatSub> allUnitList = new ArrayList<>();
    private BaseAdapter unitAdapter;
    private ItemCatSub selectUnit;
    private TextView tvCity;
    private TextView tvNote;
    private TextView tvMoney;
    private long cityCode;
    private String introduce;
    private String price;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("发布陪游").setRightText("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (0 == cityCode) {
                    showToast("请选择城市");
                    return;
                }
                if (isEmpty(introduce)) {
                    showToast("请填写介绍");
                    return;
                }
                if (isEmpty(price)) {
                    showToast("请填写价格");
                    return;
                }
                if (selectUnit == null) {
                    showToast("请选择价格单位");
                    return;
                }
                showLog(selectUnit);


                final Map<String, Object> map = new HashMap<>();
                map.put("cityCode", cityCode);
                map.put("introduce", introduce);
                map.put("lat", CurrentInfo.getUserInfo().lat);
                map.put("lng", CurrentInfo.getUserInfo().lng);
                map.put("price", price);
                map.put("type", selectUnit.value);

                List<String> images = imageAdder.getImages();
                if (!images.isEmpty()) {
                    showDialog();
                    List<ParamsUtil.ParamsTwo<OssClient.FileType, String>> twoList = new ArrayList<>();
                    for (String image : images) {
                        twoList.add(ParamsUtil.getInstance().create(OssClient.FileType.Image, image));
                    }
                    OssClient.getInstance().update(activity, twoList, new OssClient.UpdatesListener() {
                        @Override
                        public void onFinish(final boolean isSuccess, List<OssResult> results, final List<String> ossUrls) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (isSuccess) {
                                        map.put("images", ImageApi.getImageParams(ossUrls));
                                        update(map);
                                    } else {
                                        showToast("图片上传失败");
                                    }
                                }
                            });
                        }
                    });
                } else {
                    update(map);
                }


            }
        });
        setContentView(R.layout.activity_publish_lvxin);
        initView();
        loadData();
    }

    private void update(Map<String, Object> map) {
        LvXinHttp.createPei(map, new BaseEntityOb<String>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, String data, String msg) {
                showToast(msg);
                hideDialog();
                setResult(RESULT_OK);
                destroyActivity();
            }
        });
    }

    private void initView() {
        tvCity = findViewById(R.id.tvCity);
        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(CityChooseActivity.class, 10001);
            }
        });

        tvNote = findViewById(R.id.tvNote);
        tvNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, EditTextActivity.class);
                intent.putExtra("data", tvNote.getText().toString().trim());
                jump2Activity(intent, 3);
            }
        });
        tvMoney = findViewById(R.id.tvMoney);
        tvMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, EditNumActivity.class);
                intent.putExtra("data", tvMoney.getText().toString().trim());
                jump2Activity(intent, 4);
            }
        });
        imageAdder = findViewById(R.id.imageAdder);
        imageAdder.bindContext(this, 6);

        recycler3 = findViewById(R.id.recycler3);
        recycler3.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));

    }

    private void loadData() {
        List<ItemCatSub> units = CatUtils.getInstance().getCatListByCode("service-unit-type");
        initUnitAdapter(units);
//        FuWuHttp.catUnitList(new BaseEntityOb<Map<String, List<ItemCatSub>>>() {
//            @Override
//            public void onDataDeal(boolean isSuccess, Map<String, List<ItemCatSub>> data, String msg) {
//                List<ItemCatSub> units = null;
//                if (data != null) {
//                    units = data.get("unit");
//                }
//                initUnitAdapter(units);
//            }
//        });
    }

    private void initUnitAdapter(List<ItemCatSub> data) {
        allUnitList.clear();
        if (data != null) {
            allUnitList.addAll(data);
        }
        if (unitAdapter == null) {
            unitAdapter = new BaseAdapter<ItemCatSub, BaseHolder>(R.layout.item_layout_cat_uint, allUnitList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemCatSub catOpt = allUnitList.get(position);
                    ImageView iv = helper.getView(R.id.iv);
                    if (catOpt.equals(selectUnit)) {
                        iv.setImageResource(R.drawable.icon_checkbox_checked);
                    } else {
                        iv.setImageResource(R.drawable.icon_checkbox);
                    }
                    TextView tv = helper.getView(R.id.tv);
                    tv.setText(catOpt.description);
                }
            };
            unitAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    selectUnit = allUnitList.get(position);
                    unitAdapter.notifyDataSetChanged();
                }
            });
            recycler3.setAdapter(unitAdapter);
        }
        unitAdapter.notifyDataSetChanged();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == Activity.RESULT_OK) {
            imageAdder.onImagePickResult(requestCode, resultCode, intent);
            if (requestCode == 10001) {
                //城市切换
                Serializable data = intent.getSerializableExtra("data");
                if (data != null && data instanceof City) {
                    City selectCity = (City) data;
                    tvCity.setText(selectCity.name);
                    cityCode = selectCity.id;
                }
            } else if (requestCode == 3) {
                String data = intent.getStringExtra("data");
                tvNote.setText(data);
                introduce = data;
            } else if (requestCode == 4) {
                String data = intent.getStringExtra("data");
                tvMoney.setText(data);
                price = data;
            }
        }
    }

}